package org.upupxjg.xlog.template;

import com.petebevin.markdown.MarkdownProcessor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: xiaojiangang
 * Date: 13-6-22
 * Time: 下午9:33
 */
public class TemplateProcessor {
    private String templatePath;
    private static final MarkdownProcessor markDownProcessor = new MarkdownProcessor();
    public TemplateProcessor(String templatePath){
        this.templatePath = templatePath+"/";
    }
    public boolean hasFile(String filename){
        if(filename.equals("/")){
            return false;
        }
        File file = new File(templatePath+filename);
        return file.exists()&&file.isFile();
    }
    public String getFileProcessed(String filename){
        StringBuilder sb = new StringBuilder();
        try {
            FileReader fileReader = new FileReader(new File(templatePath+filename));
            char[] buffer = new char[4096];
            while (fileReader.read(buffer) > 0){
                sb.append(buffer);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return markDownProcessor.markdown(sb.toString());
    }
}
